package com.franklin.ideaplugin.easytesting.ui;

import cn.hutool.core.lang.Pair;
import com.franklin.ideaplugin.api.beans.HeaderMapping;
import com.franklin.ideaplugin.api.ui.table.AbstractTableModel;
import com.intellij.ui.components.JBCheckBox;
import lombok.RequiredArgsConstructor;

import javax.swing.*;

/**
 * @author Ye Junhui
 * @since 2023/5/29
 */
@RequiredArgsConstructor
public class HttpHeaderTableModel extends AbstractTableModel<HeaderMapping> {

    private final JBCheckBox jbCheckBox;
    private final JTable jTable;

    @Override
    protected String[] headerNames() {
        return new String[]{"","Header Name","Header Value"};
    }

    @Override
    protected Object[] toArray(HeaderMapping rowData) {
        return new Object[]{rowData.getEnable(),rowData.getKey(),rowData.getValue()};
    }

    @Override
    protected <E> void setVal(HeaderMapping rowData, int columnIndex, E value) {
        if (columnIndex == 0){
            Boolean enable = Boolean.valueOf(value.toString());
            rowData.setEnable(enable);
            boolean notSelected = this.getRowList().stream()
                    .map(HeaderMapping::getEnable)
                    .anyMatch(status -> status == Boolean.FALSE);
            this.jbCheckBox.setSelected(!notSelected);
            this.jTable.getTableHeader().repaint();
        }
        if (columnIndex == 1){
            rowData.setKey(value.toString());
        }
        if (columnIndex == 2){
            rowData.setValue(value.toString());
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        if (columnIndex == 0){
            return Boolean.class;
        }
        return String.class;
    }


}
